{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# preparation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## add the path of repository, import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(r'D:\\ZiLab\\calibrator_zurich')\n",
    "\n",
    "import numpy as np\n",
    "import time\n",
    "import os\n",
    "from importlib import reload\n",
    "from pylab import *\n",
    "\n",
    "from calibrator import interface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reload(interface)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## change the path (optional)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interface.exp_data_dir = r'M:\\Experimental Data'\n",
    "interface.calibration_session = 'Zurich Calibration'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- open spectrum analyzer (also scalabrad by GUI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.system(\"start cmd /k ipython3 M:\\calibrator_zurich\\calibrator\\servers\\spectrum_analyzer.py\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Note: do not connect interface many times, may induce visa error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user = interface.user_interface()\n",
    "\n",
    "user.switch_port('dev8334', 0)\n",
    "print(user.dv.cd())\n",
    "# ['', 'Zurich Calibration', 'dev8334', 'awg0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    user.add_new_device(board_name ='dev8334')\n",
    "except:\n",
    "    print(\"These run only at first time for a given device\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- check the path and microwave source\n",
    "- If failed, check the microwave source in NI-visa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user.update_LO(LO_id = None)\n",
    "# LO_id = None, then LO_id is from the registry\n",
    "#  Note: do not connect interface many times, may induce visa error\n",
    "# LO_Server = user.LO_Server"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## zero calibration\n",
    "the data will appear in the path you have just set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "user.cali_zero(freq=np.arange(4.0, 7.001, 0.005), noisy=True, comment='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## redo wrong points (optional)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = interface.get_dataset(2, user.board_name, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx_wrong = np.where(data[:,-1]>-80)\n",
    "freq_wrong=data[:,0][idx_wrong]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "freq_wrong"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user.cali_zero(freq=freq_wrong, noisy=True, comment='redo 00002')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_corr = interface.get_dataset(5, user.board_name, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_mix = data.copy()\n",
    "data_mix[idx_wrong[0]] = data_corr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interface.save_zero(user.dv, data_mix, comment='mix from 00002 and 00005')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test the performance (optional)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user.awg_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = interface.get_dataset(353, user.board_name, user.awg_index)\n",
    "# awg_index = n (0,1,2,3)\n",
    "# means ports 2n as I and 2n+1 as Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data[-1] = array([ 4.02000000e+00, -5.74951172e-02,  3.72619629e-02, -9.49850867e+01])\n",
    "fc_test, off_I, off_Q = data[0,:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user.LO_Server.select_device('TCPIP0::192.168.1.240::inst0::INSTR')\n",
    "user.LO_Server.frequency(fc_test*1e3) # MHz\n",
    "user.LO_Server.output(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user.fpga.dc_offset(dc_I=0., dc_Q=0.)\n",
    "time.sleep(0.1)\n",
    "(freqs, dBs) = user.scan_spectrum(f_center=fc_test, f_span=0.05)\n",
    "\n",
    "user.fpga.dc_offset(dc_I=off_I, dc_Q=off_Q)\n",
    "time.sleep(0.1)\n",
    "(freqs2, dBs2) = user.scan_spectrum(f_center=fc_test, f_span=0.05)\n",
    "\n",
    "plt.plot(freqs,dBs,'k-',label='before cali')\n",
    "plt.plot(freqs2,dBs2,'r-',label='after cali')\n",
    "plt.legend()\n",
    "plt.xlabel('frequency/GHz')\n",
    "plt.ylabel('power/dB')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Here is an example of zero correction performance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1_example = np.array([\n",
    "    -71.5, -65.9, -69.7, -67.1, -66.5, -67.8, -67.3, -67.9, -67.8,\n",
    "    -72.6, -68.7, -69.9, -68.9, -68. , -72.5, -68.5, -69. , -67.6,\n",
    "    -69.3, -73.1, -68.1, -70.7, -68.5, -69.3, -45.9, -34.9, -45.7,\n",
    "    -68.7, -70.8, -68.2, -67.3, -71.2, -72.3, -67.6, -67.2, -72. ,\n",
    "    -69.9, -68.3, -73.4, -70.1, -68.3, -68.5, -67.7, -68.5, -69.9,\n",
    "    -65.1, -68.9, -66.5, -67.6, -70.6, -69.2\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2_example = np.array([\n",
    "    -69.1, -71.8, -68.6, -70.9, -69.9, -70.4, -67. , -68.6, -67.4,\n",
    "    -66.7, -64.8, -69.8, -69.6, -70.3, -67.9, -67.3, -71.4, -67.1,\n",
    "    -70.2, -69.4, -69. , -70.2, -68.3, -69.2, -67.1, -69.2, -67.3,\n",
    "    -67.6, -67.9, -67.5, -69.5, -70.3, -68.8, -67.5, -67.2, -68.6,\n",
    "    -67. , -69.4, -67.8, -70.8, -67.8, -70.1, -68.9, -67.3, -67.8,\n",
    "    -67.2, -68.6, -68. , -70.7, -69.7, -69.2\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "freqs = np.arange(3.975,4.026,0.001)\n",
    "\n",
    "plt.plot(freqs,data1_example,'k-',label='before cali')\n",
    "plt.plot(freqs,data2_example,'r-',label='after cali')\n",
    "plt.legend()\n",
    "plt.xlabel('frequency/GHz')\n",
    "plt.ylabel('power/dB')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
